package org.academic.texteditor.tool;

import java.awt.Dimension;
import java.awt.Insets;
import java.awt.Rectangle;
import java.io.Serializable;

import org.academic.texteditor.gui.feature.Page;
import org.academic.texteditor.util.Unit;

public class Margin implements Serializable {

	private static final long serialVersionUID = -2753942706611891932L;
	
	private float top;
	private float bottom;
	private float left;
	private float right;
	
	public Margin() {
		
	}

	public Margin(float top, float left, float right, float bottom) {
		this.top = top;
		this.bottom = bottom;
		this.left = left;
		this.right = right;
	}

	public float getTop() {
		return top;
	}

	public void setTop(float top) {
		this.top = top;
	}

	public float getBottom() {
		return bottom;
	}

	public void setBottom(float bottom) {
		this.bottom = bottom;
	}

	public float getLeft() {
		return left;
	}

	public void setLeft(float left) {
		this.left = left;
	}

	public float getRight() {
		return right;
	}

	public void setRight(float right) {
		this.right = right;
	}
	
	public Insets getMargin(Page page) {
		int width = 5;
		
		int top = (int) Unit.CENTIMETER.convert(this.top) + width;
		int left = (int) Unit.CENTIMETER.convert(this.left) + width;
		int bottom = (int) Unit.CENTIMETER.convert(this.bottom) + width;
		int right = (int) Unit.CENTIMETER.convert(this.right) + width;
		
		return new Insets(top, left, bottom, right);
	}
	
	public Rectangle getRectangle(Page page) {
		Dimension size = page.getSize();
		
		int x = (int) Unit.CENTIMETER.convert(left);
		int y = (int) Unit.CENTIMETER.convert(top);
		int width = (int) (size.width - Unit.CENTIMETER.convert(right) - x);
		int height = (int) (size.height - Unit.CENTIMETER.convert(bottom) - y);
		
		Rectangle rect = new Rectangle(x, y, width, height);
		return rect;
	}

}
